<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2022/8/9
 * Time: 11:33
 */

namespace app\project\controller;


use app\common\service\PublicService;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class AdvanceApply extends Controller
{
    //列表
    public function index(){

        $typeId = $_GET['typeId'];
        $proId = $_GET['proId'];
        $assign['typeId'] = $typeId;
        $assign['proId'] = $proId;
        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $where['pro_id'] = $_GET['proId'];
                $where['type_id'] = $_GET['typeId'];
                $returnBlack = PublicService::ListService("project_pro_advance_apply",$where,null,$page,$limit);
                $list = $returnBlack['list'];

                $statusArray = [0=>"待审核",1=>"审核通过",2=>"驳回"];
                foreach ($list as $k=>$v){
                    $list[$k]['status'] = empty($statusArray[$v['status']])?"":$statusArray[$v['status']];
                }

                $count = $returnBlack['count'];
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }
        $this->assign($assign);
        return $this->fetch("index");
    }

    //详情
    public function detail(){
        $id = $_REQUEST['id'];
        $where['id'] = $id;
        $detail = PublicService::detailService("project_pro_advance_apply",$where);
        $this->assign("detail",$detail);
        return view("detail");
    }

    //操作
    public function save(){
        try{
            $data = $_POST;
            $detail = PublicService::detailService("project_pro_advance_apply",['id'=>$data['id']]);
            if(!empty($detail['status'])){
                throw new Exception("已审核，请勿重复操作");
            }
            //审核通过
            if($data['status']==1){
                //更新对应计划的计划时间
                $update['time'] = $data['delay'];
                $where['pro_id'] = $data['pro_id'];
                $where['type_id'] = $data['type_id'];
                Db::name("project_pro_advance_plan")->where($where)->update($update);
            }
            $data['adopttime'] = date("Y-m-d H:i:s");
            $data['adopt_id'] =  Session::get("id");
            PublicService::saveService("project_pro_advance_apply",$data,1);
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //删除
    public function del(){
        try{
            $id = $_POST['id'];
            $where['id'] = ['in',$id];
            PublicService::delService("project_pro_advance_apply",$where);
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }
}